package com.wazzapps.sdk.api;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.wazzapps.sdk.WazzUserInfo;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class Connector {
    private static final String LOG_TAG = "WazzSdk";
    private static final String PLATFORM = "android";
    private static final String STAT_URL = "https://stat.ad.wazzapps.org/";
    private String country;
    private String locale;
    private String packageName;
    private String sdkToken;
    private WazzUserInfo user;

    public Connector(String str, String str2, String str3, String str4, WazzUserInfo wazzUserInfo) {
        this.sdkToken = str;
        this.packageName = str2;
        this.locale = str3;
        this.country = str4;
        this.user = wazzUserInfo;
    }

    public void sendStatMessage(Event... eventArr) {
        new SendStat().execute(eventArr);
    }

    public String getLocale() {
        return this.locale;
    }

    public String getCountry() {
        return this.country;
    }

    /* access modifiers changed from: private */
    public class SendStat extends AsyncTask<Event, String, Integer> {
        private SendStat() {
        }

        /* access modifiers changed from: protected */
        public Integer doInBackground(Event... eventArr) {
            ArrayList arrayList = new ArrayList();
            for (Event event : eventArr) {
                String base64 = event.getBase64();
                if (base64 != null) {
                    arrayList.add(base64);
                }
            }
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(Connector.STAT_URL).openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "application/json");
                httpURLConnection.setRequestProperty("X-APP-PACKAGE", Connector.this.packageName);
                httpURLConnection.setRequestProperty("X-APP-TOKEN", Connector.this.sdkToken);
                httpURLConnection.setRequestProperty("X-ADV-ID", Connector.this.user.getUserId());
                httpURLConnection.setRequestProperty("X-APP-LOCALE", Connector.this.getLocale());
                httpURLConnection.setRequestProperty("X-APP-COUNTRY", Connector.this.getCountry().toLowerCase());
                httpURLConnection.setRequestProperty("X-APP-PLATFORM", "android");
                httpURLConnection.connect();
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                bufferedWriter.write(TextUtils.join("$", arrayList));
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                int responseCode = httpURLConnection.getResponseCode();
                if (responseCode != 200) {
                    Log.e(Connector.LOG_TAG, "Error while sending stat: response code " + responseCode);
                }
            } catch (Exception e) {
                Log.e(Connector.LOG_TAG, "Error while sending stat: " + e.getMessage());
            }
            return 1;
        }
    }
}